/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgameslogic;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 75
 */
/**
 *
 * Class to store all the static information as the modalities, disciplines and
 * discipline events that are configured in the system. In each specific OG
 * edition there will be a linked list of EditionDisciplineEvents
 *
 * It will provide methods to search, delete and create information Recent
 * requirements from the client made us consider that all the athletes
 * information is also considered the same from edition to edition
 */
public class ApplicationData implements Serializable {
   
   private LinkedList<Edition> editions = new LinkedList();
   private LinkedList<Nation> nations = new LinkedList();
   private LinkedList<Athlete> athletes = new LinkedList();
   private LinkedList<Team> teams = new LinkedList();
   private LinkedList<Modality> modalities = new LinkedList();
   private LinkedList<Discipline> disciplines = new LinkedList();
   private LinkedList<DisciplineEvent> disciplineEvents = new LinkedList();
   private LinkedList<AppUser> appUsers = new LinkedList();
   private LinkedList<Language> idioma = new LinkedList();
   private AppUser utilizador;
   private Language ling;
   private boolean triggerRefresh = false;

   /**
    * @return the nations
    */
   public LinkedList<Nation> getNations() {
      return nations;
   }

   /**
    *
    * @param n
    */
   public void addNation(Nation n) {
      nations.add(n);
   }

   /**
    *
    * @param e
    */
   public void addEdition(Edition e) {
      editions.add(e);
   }

   /**
    *
    * @return
    */
   public LinkedList<Edition> getEditions() {
      return editions;
   }

   /**
    *
    * @param m
    */
   public void addModality(Modality m) {
      modalities.add(m);
   }

   /**
    *
    * @param code
    * @return
    */
   public Nation getNation(String code) {
      
      for (int i = 0; i < getNations().getSize(); i++) {
	 Nation n = (Nation) getNations().getElement(i);
	 if (n.getCode().equalsIgnoreCase(code)) {
	    return n;
	 }
      }
      return null;
      
   }

   /**
    * @return the modalities
    */
   public LinkedList<Modality> getModalities() {
      return modalities;
   }

   /**
    * @param modalities the modalities to set This method will import from all
    * modalities, discipline and discipline events from a csv file according to
    * a specific layout. It still doesn't allow to define the csv file format It
    * will receive a linked list of modalities but with their relationship to
    * disciplines and events already defined.
    *
    * Nevertheless it also takes that list and separates all of them to separate
    * linkedlist to allow for a more efficient search.
    *
    */
   public void setModalities(LinkedList<Modality> modalities) {
      
      
      for (Modality modality : modalities) {
	 this.modalities.add(modality);
	 for (Discipline d : modality.getDisciplines()) {
	    getDisciplines().add(d);
	    for (DisciplineEvent de : d.getEventsList()) {
	       getDisciplineEvents().add(de);
	    }
	    
	 }
	 
      }
      
      
      
   }

   /**
    *
    * @return
    */
   public LinkedList<Discipline> getDisciplines() {
      return disciplines;
   }

   /**
    *
    * @return
    */
   public LinkedList<DisciplineEvent> getDisciplineEvents() {
      return disciplineEvents;
   }

   /**
    *
    * @return
    */
   @Override
   public String toString() {
      return "the system has now the folowing items configured:\n"
	      + "Nations:\n " + getNations() + "\nModalities,Disciplines and events:\n" + getModalities();
   }

   /**
    *
    * @param editionYear
    * @return
    */
   public Edition getEdition(int editionYear) {
      for (Edition edition : editions) {
	 if (edition.getYear() == editionYear) {
	    return edition;
	 }
	 
      }
      
      
      
      
      
      
      return null;
      
   }

   /**
    *
    * @param modality
    * @return
    */
   public Modality getModality(String modality) {
      
      for (Modality m : modalities) {
	 if (m.getName().trim().equalsIgnoreCase(modality.trim())) {
	    return m;
	 }
	 
      }
      
      return null;
   }

   /**
    * @return the athletes
    */
   public LinkedList<Athlete> getAthletes() {
      return athletes;
   }

   /**
    *
    * @return
    */
   public LinkedList<AppUser> getAppUsers() {
      return appUsers;
   }

   /**
    * @param editions the editions to set
    */
   public void setEditions(LinkedList<Edition> editions) {
      this.editions = editions;
   }

   /**
    * @param nations the nations to set
    */
   public void setNations(LinkedList<Nation> nations) {
      this.nations = nations;
   }

   /**
    * @param athletes the athletes to set
    */
   public void setAthletes(LinkedList<Athlete> athletes) {
      this.athletes = athletes;
   }

   /**
    * @param appUsers the appUsers to set
    */
   public void setAppUsers(LinkedList<AppUser> appUsers) {
      this.appUsers = appUsers;
   }

   /**
    * @return the utilizador
    */
   public AppUser getUtilizador() {
      return utilizador;
   }

   /**
    * @param utilizador the utilizador to set
    */
   public void setUtilizador(AppUser utilizador) {
      this.utilizador = utilizador;
   }

   /**
    *
    * @param modality
    * @param discipline
    * @param gender
    * @param type
    * @return
    */
   public boolean isAvailableEvent(String modality, String discipline, DisciplineEvent.EVENTGENDER gender, Discipline.DISCIPLINETYPE type) {
      for (Modality m : modalities) {
	 if (m.getName().equalsIgnoreCase(modality)) {
	    for (Discipline d : m.getDisciplines()) {
	       if (d.getName().equalsIgnoreCase(discipline) && d.getType().equals(type)) {
		  if (d.getDisciplineEvent(gender) != null) {
		     return true;
		  }
		  
	       }
	    }
	 }
      }
      return false;
   }

   /**
    * @return the idioma
    */
   public LinkedList<Language> getIdioma() {
      return idioma;
   }

   /**
    * @param idioma the idioma to set
    */
   public void setIdioma(LinkedList<Language> idioma) {
      this.idioma = idioma;
   }

   /**
    * @return the ling
    */
   public Language getLing() {
      return ling;
   }

   /**
    * @param ling the ling to set
    */
   public void setLing(Language ling) {
      this.ling = ling;
      
   }

   /**
    *
    * @param athlete
    */
   public void addAthlete(Athlete athlete) {
      athletes.add(athlete);
   }

   /**
    *
    * @param athleteName
    * @param n
    * @return
    */
   public Athlete getAthlete(String athleteName, Nation n) {
      for (Athlete a : athletes) {
	 if (a.getName().trim().equalsIgnoreCase(athleteName) && a.getNation() == n) {
	    return a;
	 }
      }
      return null;
   }

   /**
    *
    * @param estado
    */
   public void setTriggerRefresh(boolean estado) {
      this.triggerRefresh = estado;
   }

   /**
    *
    * @return
    */
   public boolean getTriggerRefresh() {
      return this.triggerRefresh;
      
   }

   /**
    *
    * @return
    */
   public LinkedList<Team> getTeams() {
      return teams;
   }

   /**
    *
    * @param t
    */
   public void addTeam(Team t) {
      teams.add(t);
   }

   /**
    *
    * @param editionDisciplineEvent
    */
   public void resetMedals(EditionDisciplineEvent editionDisciplineEvent) {


      //Remove as medalhas desse evento dos atletas

      for (Athlete a : athletes) {
	 
	 for (Medal m : a.getMedals()) {
	    if (m.getEditionDisciplineEvent().equals(editionDisciplineEvent)) {
	       a.getMedals().remove(m);
	    }
	 }
      }

//Remove as medalhas desse evento da nação      
      for (Nation n : nations) {
	 for (Medal m : n.getNationMedals()) {
	    if (m.getEditionDisciplineEvent().equals(editionDisciplineEvent)) {
	       n.getNationMedals().remove(m);
	    }
	 }
	 
      }


      //remove as medalhas desse evento das delegações

      for (Delegation d : (getEdition(editionDisciplineEvent.getEdition().getYear())).getEditionDelegationList()) {
	 
	 for (Medal m : d.getDelegationMedals()) {
	    if (m.getEditionDisciplineEvent().equals(editionDisciplineEvent)) {
	       d.getDelegationMedals().remove(m);
	    }
	 }
	 
	 
      }
      
      editionDisciplineEvent.setMedalsAssigned(false);
   }

   /**
    *
    * @param e
    */
   public void removeEditionMedals(Edition e) {

      //Remove as medalhas desse evento dos atletas

      for (Athlete a : athletes) {
	 
	 for (Medal m : a.getMedals()) {
	    if (m.getEdition().equals(e)) {
	       a.getMedals().remove(m);
	    }
	 }
	 
      }

//Remove as medalhas desse evento da nação      
      for (Nation n : nations) {
	 for (Medal m : n.getNationMedals()) {
	    if (m.getEdition().equals(e)) {
	       n.getNationMedals().remove(m);
	    }
	 }
	 
      }
      
      for (Delegation d : e.getEditionDelegationList()) {
	 for (Medal m : d.getDelegationMedals()) {
	    if (m.getEdition().equals(m)) {
	       d.getDelegationMedals().remove(m);
	    }
	 }
      }
   }

   /**
    *
    * @param n
    * @return
    */
   public int getGamesEntered(Nation n) {
      int counter = 0;
      for (Edition edition : editions) {
	 for (Delegation delegation : edition.getEditionDelegationList()) {
	    if (delegation.getCountry().equals(n)) {
	       counter++;
	    }
	 }
      }
      return counter;
   }

   /**
    *
    * @param n
    * @return
    */
   public int getTotalEventsParticipation(Nation n) {
      int counter = 0;
      for (Edition edition : editions) {
	 for (EditionDisciplineEvent edDisEv : edition.getEditionDisciplineEventList()) {
	    for (EditionEventParticipation edEvPart : edDisEv.getEditionEventParticipation()) {
	       if (edEvPart.getNationParticipation().equals(n)) {
		  counter++;
	       }
	    }
	 }
      }
      return counter;
   }

   /**
    *
    * @param edition
    */
   public void removeEdition(Edition edition) {
      
      edition.getEditionDisciplineEventList().clear();
      removeEditionMedals(edition);
      editions.remove(edition);
   }
}
